package com.zhs.gccs.view.taskMap;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zhs.gccs.R;
import com.zhs.gccs.adapter.GccsDataItemAdapter;
import com.zhs.gccs.adapter.GccsLevelTypeAdapter;
import com.zhs.gccs.event.EventProjectGccjViewClose;
import com.zhs.gccs.model.CaData;
import com.zhs.gccs.model.CaDict;
import com.zhs.gccs.model.CaDictDataVo;
import com.zhs.gccs.model.CaForm;
import com.zhs.gccs.model.CaFile;
import com.zhs.gccs.model.SmTask;
import com.zhs.gccs.model.StatisticsDict;
import com.zhs.gccs.model.TaskProject;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.utils.SoftKeyboardUtils;
import com.zhs.gccs.view.BaseView;
import com.zhs.gccs.view.LocationAddress;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

//工程措施抽检
public class ProjectGccsView extends BaseView {
    private TextView tv_title;
    private TextView tv_title_sub;//
    private View iv_close;//关闭
    private View tv_title_help;//
    //逐级菜单
    RecyclerView rl_level;
    private GccsLevelTypeAdapter levelAdapter;
    private List<StatisticsDict> levelData;
    private StatisticsDict currentLevel;
    //提示
    private View ll_tips;
    private GccjTipView v_tip_content;
    private View iv_close_tip;//

    //////////////////////
    Activity activity;
    TaskProject taskProject;
    SmTask smTask;
    StatisticsDict dictTypeOfTopLevel;//采集类型--大类
    ///////////////////////////////////
    View sv_form;//表单页面
    private View ll_data_history;//指标历史数据
    private RecyclerView rl_data_history;//
    private GccsDataItemAdapter gccsDataItemAdapter;
    private List<CaData> gccsDatas;
    private View ll_add_record;
    private ProjectGccsDataFormView dataFormView;//表单
    private boolean formInEdit = false;
    CaForm caFormCurrent;
    LocationAddress location;
    ///////////////////////////////////////

    public ProjectGccsView(@NonNull Context context) {
        super(context);
    }

    public ProjectGccsView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ProjectGccsView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_view_project_gccs, this, true);
//        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomerLogo);
//        String title = typedArray.getString(R.styleable.CustomerInfoItem_preText);
//        typedArray.recycle();
        tv_title = view.findViewById(R.id.tv_title);
        tv_title = view.findViewById(R.id.tv_title);
        tv_title_sub = view.findViewById(R.id.tv_title_sub);
        iv_close = view.findViewById(R.id.iv_close);
        iv_close.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                formInEdit = false;
                currentLevel = null;
                SoftKeyboardUtils.hideSoftInput(activity);
                EventBus.getDefault().post(new EventProjectGccjViewClose(taskProject));
            }
        });
        tv_title_sub.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_title_sub.setClickable(false);
                if(formInEdit){
                    SoftKeyboardUtils.hideSoftInput(activity);
                    ll_data_history.setVisibility(View.VISIBLE);
                    dataFormView.setVisibility(View.GONE);
                    formInEdit = false;
                    tv_title_sub.setClickable(true);
                    tv_title_help.setVisibility(View.GONE);
                    return;
                }
                if(currentLevel.getParent()==null){
                    gccjDataChangeListener.
                            loadDictMenu(dictTypeOfTopLevel,taskProject,
                                    new GccjDictStatisticsDataLoadListener() {
                                        @Override
                                        public void dataLoadSuccess(List<StatisticsDict> pDict) {
                                            currentLevel = null;
                                            levelData.clear();
                                            levelData.addAll(pDict);
                                            levelAdapter.notifyDataSetChanged();
                                            tv_title.setText(dictTypeOfTopLevel.getDictName());
                                            tv_title.setVisibility(View.VISIBLE);
                                            tv_title_sub.setText("");
                                            tv_title_sub.setVisibility(View.GONE);
                                            rl_level.setVisibility(View.VISIBLE);
                                            sv_form.setVisibility(View.GONE);
                                            tv_title_sub.setClickable(true);
                                            tv_title_help.setVisibility(View.GONE);
                                        }

                                        @Override
                                        public void dataLoadError() {

                                        }
                                    });
                }else  {
                    gccjDataChangeListener.
                            loadDictMenu(currentLevel.getParent(),taskProject,
                                    new GccjDictStatisticsDataLoadListener() {
                                        @Override
                                        public void dataLoadSuccess(List<StatisticsDict> pDict) {
                                            currentLevel = currentLevel.getParent();
                                            levelData.clear();
                                            levelData.addAll(pDict);
                                            levelAdapter.notifyDataSetChanged();
                                            tv_title.setText("");
                                            tv_title.setVisibility(View.GONE);
                                            tv_title_sub.setText(currentLevel.getDictName());
                                            tv_title_sub.setVisibility(View.VISIBLE);
                                            rl_level.setVisibility(View.VISIBLE);
                                            sv_form.setVisibility(View.GONE);
                                            tv_title_sub.setClickable(true);
                                            tv_title_help.setVisibility(View.GONE);
                                        }

                                        @Override
                                        public void dataLoadError() {

                                        }
                                    });
                }
            }
        });
        rl_level = view.findViewById(R.id.rl_level);
        levelData = new ArrayList<>();
        levelAdapter = new GccsLevelTypeAdapter(context, levelData);
        rl_level.setLayoutManager(new LinearLayoutManager(context,
                LinearLayoutManager.VERTICAL, false));
        rl_level.setAdapter(levelAdapter);
        levelAdapter.setOperationListener(new GccsLevelTypeAdapter.OperationListener() {
            @Override
            public void onItemClick(int position) {
                StatisticsDict statisticsDict = levelData.get(position);
                if(gccjDataChangeListener!=null){
                    if(StatisticsDict.Type_dict.equals(statisticsDict.getDictType())){
                        ////加载指标列表
                        gccjDataChangeListener.
                                loadDictMenu(statisticsDict,taskProject,
                                        new GccjDictStatisticsDataLoadListener() {
                                            @Override
                                            public void dataLoadSuccess(List<StatisticsDict> pDict) {
                                                StatisticsDict parent = currentLevel;
                                                currentLevel = statisticsDict;
                                                currentLevel.setParent(parent);
                                                levelData.clear();
                                                levelData.addAll(pDict);
                                                levelAdapter.notifyDataSetChanged();
                                                tv_title.setText("");
                                                tv_title.setVisibility(View.GONE);
                                                tv_title_sub.setText(statisticsDict.getDictName());
                                                tv_title_sub.setVisibility(View.VISIBLE);
                                                rl_level.setVisibility(View.VISIBLE);
                                                sv_form.setVisibility(View.GONE);
                                                tv_title_help.setVisibility(View.GONE);
                                            }

                                            @Override
                                            public void dataLoadError() {

                                            }
                                        });
                    }else if(StatisticsDict.Type_form.equals(statisticsDict.getDictType())){
                        //表单项点击了，加载数据列表
                        gccjDataChangeListener.loadDataList(statisticsDict,
                                taskProject,new GccjDataListLoadListener(){

                            @Override
                            public void dataLoaded(List<CaData> caDatas) {
                                StatisticsDict parent = currentLevel;
                                currentLevel = statisticsDict;
                                currentLevel.setParent(parent);
                                gccsDatas.clear();
                                if(caDatas!=null)gccsDatas.addAll(caDatas);
                                gccsDataItemAdapter.notifyDataSetChanged();
                                tv_title.setText("");
                                tv_title.setVisibility(View.GONE);
                                tv_title_sub.setText(statisticsDict.getDictName());
                                tv_title_sub.setVisibility(View.VISIBLE);
                                rl_level.setVisibility(View.GONE);
                                sv_form.setVisibility(View.VISIBLE);
                                tv_title_help.setVisibility(View.GONE);
                            }
                        });
                    }
                }
            }
        });

        sv_form = view.findViewById(R.id.sv_form);
        ll_add_record = view.findViewById(R.id.ll_add_record);
        ll_data_history = view.findViewById(R.id.ll_data_history);
        rl_data_history  = view.findViewById(R.id.rl_data_history);
        //历史数据列表
        gccsDatas = new ArrayList<>();
        gccsDataItemAdapter = new GccsDataItemAdapter(context, gccsDatas);
        rl_data_history.setLayoutManager(new LinearLayoutManager(context,
                LinearLayoutManager.VERTICAL, false));
        rl_data_history.setAdapter(gccsDataItemAdapter);
        gccsDataItemAdapter.setOperationListener(new GccsDataItemAdapter.OperationListener() {
            @Override
            public void onItemClick(int position) {
                CaData caData = gccsDatas.get(position);
                gccjDataChangeListener.loadFormDataDetail(currentLevel,taskProject,caData,new GccjFormDataListener(){
                    @Override
                    public void dataLoaded(CaForm caForm) {
                        formInEdit = true;
                        caFormCurrent = caForm;
                        ll_data_history.setVisibility(View.GONE);
                        dataFormView.loadData(activity,taskProject,smTask,
                                dictTypeOfTopLevel, currentLevel,caForm);
                        dataFormView.setVisibility(View.VISIBLE);
                        tv_title_help.setVisibility(View.VISIBLE);
                    }
                });
            }
        });

        dataFormView = view.findViewById(R.id.form_data);
        //新增
        ll_add_record.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                gccjDataChangeListener.loadFormDataDetail(currentLevel,taskProject,null,new GccjFormDataListener(){
                    @Override
                    public void dataLoaded(CaForm caForm) {
                        formInEdit = true;
                        caFormCurrent = caForm;
                        ll_data_history.setVisibility(View.GONE);
                        //新增
                        dataFormView.loadData(activity,taskProject,smTask,
                                dictTypeOfTopLevel, currentLevel,caForm);
                        dataFormView.setVisibility(View.VISIBLE);
                        tv_title_help.setVisibility(View.VISIBLE);
                    }
                });
            }
        });
        dataFormView.setDataFormListener(new ProjectGccsDataFormView.DataFormListener() {

            @Override
            public void onCancel() {
                ll_data_history.setVisibility(View.VISIBLE);
                dataFormView.setVisibility(View.GONE);
                tv_title_help.setVisibility(View.GONE);
                formInEdit = false;
            }

            @Override
            public void onSave(TaskProject taskProject, SmTask smTask, StatisticsDict caType,
                               StatisticsDict currentLevel, CaDictDataVo dictDataVo,
                               List<CaFile> photos,
                               List<CaFile> videos) {
                if(gccjDataChangeListener!=null)gccjDataChangeListener.
                        saveGccjData(taskProject,smTask,
                        caType,currentLevel,
                        dictDataVo,photos,videos,new GccjFormDataSaveListener(){

                                    @Override
                                    public void dataLoaded() {
                                        //加载列表数据
                                        gccjDataChangeListener.loadDataList(currentLevel,
                                                taskProject,new GccjDataListLoadListener(){

                                                    @Override
                                                    public void dataLoaded(List<CaData> caDatas) {
                                                        gccsDatas.clear();
                                                        if(caDatas!=null)gccsDatas.addAll(caDatas);
                                                        gccsDataItemAdapter.notifyDataSetChanged();
                                                        tv_title.setText("");
                                                        tv_title.setVisibility(View.GONE);
                                                        tv_title_sub.setText(currentLevel.getDictName());
                                                        tv_title_sub.setVisibility(View.VISIBLE);
                                                        rl_level.setVisibility(View.GONE);
                                                        sv_form.setVisibility(View.VISIBLE);
                                                    }
                                                });
                                        ll_data_history.setVisibility(View.VISIBLE);
                                        dataFormView.setVisibility(View.GONE);
                                        tv_title_help.setVisibility(View.GONE);
                                        formInEdit = false;
                                    }
                                });
            }
        });


        ll_tips = view.findViewById(R.id.ll_tips);
        iv_close_tip = view.findViewById(R.id.ll_tips);
        v_tip_content = view.findViewById(R.id.v_tip_content);

        iv_close_tip.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                ll_tips.setVisibility(View.GONE);
            }
        });
        ll_tips.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                ll_tips.setVisibility(View.GONE);
            }
        });
        tv_title_help = view.findViewById(R.id.tv_title_help);
        tv_title_help.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                SoftKeyboardUtils.hideSoftInput(activity);
                if(dataFormView!=null&&v_tip_content!=null){
                    CaDict cjslDict = dataFormView.getDictByCode(CaDict.key_cjsl);
                    CaDict cjffDict = dataFormView.getDictByCode(CaDict.key_cjff);
                    CaDict cjpjbzDict = dataFormView.getDictByCode(CaDict.key_cjpjbz);
                    v_tip_content.load(cjslDict,cjffDict,cjpjbzDict);
                }
                ll_tips.setVisibility(View.VISIBLE);
            }
        });
    }


    public void loadData(Activity activity, TaskProject taskProject, SmTask smTask,
                         StatisticsDict caType,
                         List<StatisticsDict> caTypeSubMenus) {
        this.activity = activity;
        this.taskProject = taskProject;
        this.smTask = smTask;
        this.dictTypeOfTopLevel = caType;
        this.currentLevel = null;
        levelData.clear();
        levelData.addAll(caTypeSubMenus);
        levelAdapter.notifyDataSetChanged();
        tv_title.setText(caType.getDictName());
        tv_title.setVisibility(View.VISIBLE);
        tv_title_sub.setText("");
        tv_title_sub.setVisibility(View.GONE);
        rl_level.setVisibility(View.VISIBLE);
        sv_form.setVisibility(View.GONE);
        ll_data_history.setVisibility(View.VISIBLE);
        dataFormView.setVisibility(View.GONE);
        ll_tips.setVisibility(View.GONE);
    }


    public interface GccjDataChangeListener{
        void loadDataList(StatisticsDict pDict,TaskProject taskProject,GccjDataListLoadListener listener);
        //加载指标列表
        void loadDictMenu(StatisticsDict pDict, TaskProject taskProject, GccjDictStatisticsDataLoadListener listener);
        void saveGccjData(TaskProject taskProject, SmTask smTask, StatisticsDict caType,
                          StatisticsDict currentLevel,
                          CaDictDataVo dictDataVo,
                          List<CaFile> photos, List<CaFile> videos,
                          GccjFormDataSaveListener listener);

        void loadFormDataDetail(StatisticsDict currentLevel, TaskProject taskProject, CaData caData, GccjFormDataListener gccjDataListLoadListener);
    }
    public interface GccjDictStatisticsDataLoadListener {
        void dataLoadSuccess(List<StatisticsDict>  pDict);
        void dataLoadError();
    }
    public interface GccjDataListLoadListener{
        void dataLoaded(List<CaData>  pDict);
    }
    public interface GccjFormDataListener{
        void dataLoaded(CaForm caForm);
    }

    public interface GccjFormDataSaveListener{
        void dataLoaded();
    }

    private GccjDataChangeListener gccjDataChangeListener;


    public GccjDataChangeListener getGccjDataChangeListener() {
        return gccjDataChangeListener;
    }

    public void setGccjDataChangeListener(GccjDataChangeListener gccjDataChangeListener) {
        this.gccjDataChangeListener = gccjDataChangeListener;
    }

    public void setTakeImageViewListener(GccjTakeImageView.TakeImageViewListener takeImageViewListener) {
        this.dataFormView.setTakeImageViewListener(takeImageViewListener);
    }

    public void addPhotoData(CaFile imageBean) {
        this.dataFormView.addPhotoData(imageBean);
    }

    public void addVideoData(CaFile imageBean) {
        this.dataFormView.addVideoData(imageBean);
    }

    public void addAudioData(CaFile imageBean) {
        this.dataFormView.addAudioData(imageBean);
    }

    public void setLocation(LocationAddress location) {
        this.location = location;
        if(this.dataFormView!=null&&this.dataFormView.getVisibility()==VISIBLE){
            this.dataFormView.setLocation(location);
        }
    }
}
